var busy = false
export function getLocation() {
	console.log('getLocation==========')
	return new Promise((resolve, reject) => {
		if (busy) {
			resolve({
				longitude: 0,
				latitude: 0
			})
			return
		}
		busy = true
		var loc = {
			accuracy: 65,
			altitude: 0,
			horizontalAccuracy: 65,
			latitude: 30.49984,
			longitude: 114.34253,
			speed: -1,
			verticalAccuracy: 65
		}
		let onLocationChange = loc => {
			
			console.log('onLocationChange', loc)
			uni.offLocationChange(onLocationChange)
			uni.stopLocationUpdate()
			busy = false
			resolve({
				longitude: loc.longitude,
				latitude: loc.latitude
			})
		}
		let startLocationUpdate = () => {
			uni.startLocationUpdate({
				type: 'gcj02',
				success: () => {
					uni.onLocationChange(onLocationChange)
				},
				fail: (err) => {
					console.error(err)
					busy = false
					reject(err)
				}
			})
		}
		// #ifdef MP
		uni.getSetting({
			success: res => {
				// console.log("scope.userLocation: ", res.authSetting['scope.userLocation']);
				if (!res.authSetting['scope.userLocation']) {
					uni.authorize({
						scope: 'scope.userLocation',
						success: res => {
							startLocationUpdate()
						},
						fail: async err => {
							console.error(err);
							uni.showModal({
								title: "请先打开位置权限",
								cancelText: "取消",
								confirmText: "去打开",
								success: async (res) => {
									if (res.confirm) {
										uni.openSetting({
											success: (
												res
												) => {
												startLocationUpdate()
											},
										});
									} else {
										busy = false
										resolve(null)
									}
								}
							})
						}
					})
				} else {
					startLocationUpdate()
				}
			}
		})
		// #endif
		// #ifndef MP-WEIXIN
		startLocationUpdate()
		// #endif
	})
}

export function saveImage(imgUrl) {
	// console.log("imgUrl", imgUrl)
	return new Promise((resolve, reject) => {
		uni.getImageInfo({
			src: imgUrl,
			success: (res) => {
				console.log('getImageInfo', res)
				uni.saveImageToPhotosAlbum({
					filePath: res.path,
					success: (res1) => {
						resolve()
					},
					fail: (err) => {
						uni.showModal({
							content: JSON.stringify(err)
						})
						reject(err)
					}
				})
			},
			fail: (err) => {
				uni.showModal({
					content: JSON.stringify(err)
				})
				reject(err)
			}
		})
	})
}

export function saveImages(imgs = []) {
	var i = 0
	return new Promise((resolve, reject) => {
		uni.getSetting({
			success: async (res) => {
				if (res.authSetting['scope.writePhotosAlbum'] === true) {
					try{
						var idx = 0
						for (var i = 0; i < imgs.length; i++) {
							let img = imgs[i]
							await saveImage(img)
							idx = i
						}
						resolve()
					}catch(e){
						reject({
							err: e,
							idx: idx
						})
					}
				} else if (res.authSetting['scope.writePhotosAlbum'] === false) {
					reject('未授权')
				} else {
					uni.authorize({
						scope: 'scope.writePhotosAlbum',
						success: async () => {
							try{
								var idx = 0
								for (var i = 0; i < imgs.length; i++) {
									let img = imgs[i]
									await saveImage(img)
									idx = i
								}
								resolve()
							}catch(e){
								reject({
									err: e,
									idx: idx
								})
							}
						},
						fail: (err) => {
							console.error(err)
							uni.showModal({
								content: JSON.stringify(err)
							})
							reject(err)
						}
					})
				}
			}
		})
	})
}

export function getSettings() {
	return new Promise((resolve, reject) => {
		uni.getSetting({
			withSubscriptions: true,
			success: (res) => {
				resolve(res);
			},
			fail: (err) => {
				reject(err);
			}
		})
	});
}

export function isMainSwitchOn() {
	return new Promise((resolve, reject) => {
		uni.getSetting({
			withSubscriptions: true,
			success: (res) => {
				resolve(res.subscriptionsSetting.mainSwitch);
			},
			fail: (err) => {
				reject(err);
			}
		})
	})
}

export function isMsgSwitchOn(templateId) {
	return new Promise((resolve, reject) => {
		uni.getSetting({
			withSubscriptions: true,
			success: (res) => {
				let mainSwitchOn = res.subscriptionsSetting.mainSwitch;
				if (!mainSwitchOn) {
					resolve(false);
					return;
				}
				let itemSettings = res.subscriptionsSetting.itemSettings;
				if (!itemSettings || itemSettings.length === 0) {
					resolve(false);
					return;
				}
				let setting = itemSettings[templateId];
				if (!setting) {
					resolve(false);
					return;
				}
				resolve(setting === "accept");
			},
			fail: (err) => {
				reject(err);
			}
		})
	})
}

export function hasPermisson(templateId) {
	return new Promise((resolve, reject) => {
		uni.getSetting({
			withSubscriptions: true,
			success: (res) => {
				let mainSwitchOn = res.subscriptionsSetting.mainSwitch;
				if (!mainSwitchOn) {
					resolve(false);
					return;
				}
				let itemSettings = res.subscriptionsSetting.itemSettings;
				if (!itemSettings || itemSettings.length === 0) {
					resolve(false);
					return;
				}
				let setting = itemSettings[templateId];
				if (!setting) {
					resolve(false);
					return;
				}
				resolve(true);
			},
			fail: (err) => {
				reject(err);
			}
		})
	})
}
export function requestSubscribeMsg(templateIds) {
	return new Promise((resolve, reject) => {
		uni.requestSubscribeMessage({
			tmplIds: templateIds,
			success: (res) => {
				console.log("请求订阅权限成功", res);
				//todo
				resolve(true);
			},
			fail: (err) => {
				console.log("请求订阅权限遇到错误", err);
				uni.showModal({
					title: "接收订阅消息已关闭，您将收不到重要订阅消息",
					cancelText: "取消",
					confirmText: "去打开",
					success: (res) => {
						if (res.confirm) {
							uni.openSetting({
								success: (res) => {
									console.log("设置情况", res);
									resolve(true);
								},
								fail: (err) => {
									resolve(false);
								}
							});
						} else {
							resolve(false);
						}
					}
				})
			}
		})
	})
}

export function getNavHeight() {
	let sys = uni.getSystemInfoSync();
	let statusBarH = sys.statusBarHeight;
	return statusBarH + 44;
}

export function getNavHeightMp() {
	let sys = uni.getSystemInfoSync();
	let statusBarH = sys.statusBarHeight;
	let rect = uni.getMenuButtonBoundingClientRect();
	let navH = (rect.top - statusBarH) * 2 + rect.height;
	return statusBarH + navH;
}
